Page({
  data: {
      id: null,
      cat_list: [],
      parent: 0,
      name: '',
      sort: '',
      icon: null,
      mch_id: 0
  },
  onLoad: function (t) {
      var self = this;
      getApp().page.onLoad(this, t);
      //商品id
      if (t.id) {
          this.setData({
              id: t.id
          });
          wx.setNavigationBarTitle({
              title: '分类编辑',
          })
      }
      if (t.id) {
          //分类编辑
          this.getCat(() => {
              getApp().request({
                  url: getApp().api.mch.cat.edit,
                  data: {
                      id: self.data.id
                  },
                  method: "get",
                  success: function (t) {
                      if (0 == t.code) {
                          var res = t.data;
                          console.log('cat', res)
                          console.log('cat_list', self.data.cat_list)
                          var parent = 0;
                          if (res.parent_id > 0) {
                              self.data.cat_list.forEach((item, index) => {
                                  if (item.id == res.parent_id) {
                                      parent = index
                                  }
                              })
                          }
                          self.setData({
                              parent: parent,
                              name: res.name,
                              sort: res.sort,
                              icon: res.cion
                          })
                      }
                  }
              });
          })
      } else {
          this.getCat()
      }
  },
  getCat: function (callback) {
      var self = this;
      getApp().request({
          url: getApp().api.mch.cat.get_cat,
          method: "get",
          success: function (t) {
              if (0 == t.code) {
                  t.data.list.unshift({
                      id: 0,
                      name: '无'
                  })
                  self.setData({
                      cat_list: t.data.list,
                      mch_id: t.data.mch_id
                  })
                  if (callback) {
                      callback()
                  }
              }
          }
      });
  },
  bindPickerChange: function (e) {
      this.setData({
          parent: e.detail.value
      })
  },
  add_img: function (q) {
      var t = this;
      wx.chooseImage({
          count: 1,
          sizeType: ["original", "compressed"],
          sourceType: ["album", "camera"],
          success: function (a) {
              var e = a.tempFilePaths;
              e.forEach(function (a, s) {
                  wx.showLoading({
                      type: "loading"
                  }), wx.uploadFile({
                      url: getApp().api.default.upload_image,
                      filePath: a,
                      name: "image",
                      success: function (a) {
                          wx.hideLoading()
                          var r = JSON.parse(a.data);
                          if (r.code == 0) {
                              t.setData({
                                  icon: r.data.url
                              })
                          }
                      }
                  });
              });
          }
      });
  },
  bindInput: function (e) {
      var type = e.target.dataset.type
      var value = e.detail.value
      if (type == 'name') {
          this.setData({
              name:  value
          })
      }
      if (type == 'sort') {
          this.setData({
              sort:  value
          })
      }
  },
  submit: function () {
      if (!this.data.name) {
          wx.showModal({
              title: '提示',
              content: '请填写分类名称',
              showCancel: false,
              success(res) {}
          })
          return
      }
      if (!this.data.sort) {
          wx.showModal({
              title: '提示',
              content: '请填写排序',
              showCancel: false,
              success(res) {}
          })
          return
      }
      if (this.data.id) {
          if (this.data.cat_list[this.data.parent].id == this.data.id) {
              wx.showModal({
                  title: '提示',
                  content: '不能选择自己为父级分类',
                  showCancel: false,
                  success(res) {}
              })
              return
          }
      }
      var form = {
          parent_id: this.data.cat_list[this.data.parent].id,
          name: this.data.name,
          sort: this.data.sort,
          icon: this.data.icon ? this.data.icon : '',
          mch_id: this.data.mch_id
      }
      wx.showLoading();
      getApp().request({
          url: getApp().api.mch.cat.edit,
          data: {
              form: JSON.stringify(form),
              id: this.data.id > 0 ? this.data.id : ''
          },
          method: "post",
          success: function (t) {
              wx.hideLoading(), 0 == t.code ? (wx.showToast({
                  title: t.msg,
                  icon: 'none'
              }), setTimeout(function () {
                  wx.navigateTo({
                      url: "/mch/m/cat/cat"
                  });
              }, 500)) : wx.showToast({
                  title: t.msg,
                  icon: 'none'
              });
          },
          fale: function (t) {
              wx.showToast({
                  title: t.msg,
                  icon: 'none'
              })
          }
      });
  },
  onReady: function (t) {},
  onShow: function () {},
  onHide: function () {},
  onUnload: function () {},
  onPullDownRefresh: function () {},
  onReachBottom: function () {},
  onShareAppMessage: function () {}
});